#!/usr/bin/env bash

# ----------------------------------------------------------------------
# Filename	:  passwdValid
# Version	:  1.0
# Date		:  2022/02/15
# Author	:  Lz
# Email		:  liuzuo@kylinos.com.cn 
# History	:     
#            	   Version 1.0, 2022/02/15
# Function	:  账户密码有效期
# Out		:        
#              0 => TPASS
#              1 => TFAIL
#              other=> TCONF
# ----------------------------------------------------------------------

# 测试主题
Title_Env_LTFLIB="安全加固(配置类) - 账户密码有效期"


## TODO : 个性化,初始化
#   Out : 0=>TPASS
#         1=>TFAIL
#         2=>TCONF
TestInit_LTFLIB(){
	# 判断是否存在变量
	if [ "Z${Passwd_MaxDay_Config_SSRFlag}" == "Z" ];then
		OutputRet_LTFLIB "${ERROR}"	
		TestRetParse_LTFLIB "未定义变量 Passwd_MaxDay_Config_SSRFlag"
	fi
	if [ "Z${Passwd_MinDay_Config_SSRFlag}" == "Z" ];then
		OutputRet_LTFLIB "${ERROR}"	
		TestRetParse_LTFLIB "未定义变量 Passwd_MinDay_Config_SSRFlag"
	fi
	if [ "Z${Passwd_MinLen_Config_SSRFlag}" == "Z" ];then
		OutputRet_LTFLIB "${ERROR}"	
		TestRetParse_LTFLIB "未定义变量 Passwd_MinLen_Config_SSRFlag"
	fi
	if [ "Z${Passwd_WarnAge_Config_SSRFlag}" == "Z" ];then
		OutputRet_LTFLIB "${ERROR}"	
		TestRetParse_LTFLIB "未定义变量 Passwd_WarnAge_Config_SSRFlag"
	fi

        # 判断变量是否为数字
        if [ "${Passwd_MaxDay_Config_SSRFlag}" -ge 0 ];then
                true
        else
                OutputRet_LTFLIB "${ERROR}"
                TestRetParse_LTFLIB "Passwd_MaxDay_Config_SSRFlag 非法值: ${Passwd_MaxDay_Config_SSRFlag}"
        fi
        if [ "${Passwd_MinDay_Config_SSRFlag}" -ge 0 ];then
                true
        else
                OutputRet_LTFLIB "${ERROR}"
                TestRetParse_LTFLIB "Passwd_MinDay_Config_SSRFlag 非法值: ${Passwd_MinDay_Config_SSRFlag}"
        fi
        if [ "${Passwd_MinLen_Config_SSRFlag}" -ge 0 ];then
                true
        else
                OutputRet_LTFLIB "${ERROR}"
                TestRetParse_LTFLIB "Passwd_MinLen_Config_SSRFlag 非法值: ${Passwd_MinLen_Config_SSRFlag}"
        fi
        if [ "${Passwd_WarnAge_Config_SSRFlag}" -ge 0 ];then
                true
        else
                OutputRet_LTFLIB "${ERROR}"
                TestRetParse_LTFLIB "Passwd_WarnAge_Config_SSRFlag 非法值: ${Passwd_WarnAge_Config_SSRFlag}"
        fi

        return $TPASS
}


## TODO : 清理函数
#   Out : 0=>TPASS
#         1=>TFAIL
#         2=>TCONF
TestClean_LTFLIB(){
        return $TPASS
}


## TODO : 测试用例集
#   Out : 0=>TPASS
#         1=>TFAIL
#         2=>TCONF
Testsuite_LTFLIB(){
        testcase_1
        testcase_2
        testcase_3
        testcase_4

        return $TPASS
}


## TODO ： 可使用密码最大有效期
#
testcase_1(){
	local flag_str="^PASS_MAX_DAYS"

	# 错误选项
        local false_str="${flag_str}"
	# 正确选项
       	local true_str="${flag_str}[ 	]+${Passwd_MaxDay_Config_SSRFlag}"
	
	# 查看文件中配置
	local file_conf="/etc/login.defs"
	local cmd="cat ${file_conf}"
	eval ${cmd} | grep "$flag_str"
	eval ${cmd} | grep -v "^#" | grep -Eq "${true_str}"
	CommRetParse_LTFLIB "${cmd}查询,要求${true_str}" "False"

	eval ${cmd} | grep -v "^#" | grep -Ev "${true_str}" | grep "${false_str}"
	CommRetParse_LTFLIB "${cmd}查询,不存在'${true_str}'以外的值" "False" "yes"
}


## TODO ：密码更改允许的最小间隔天数 
#
testcase_2(){
	local flag_str="^PASS_MIN_DAYS"

	# 错误选项
        local false_str="${flag_str}"
	# 正确选项
       	local true_str="${flag_str}[ 	]+${Passwd_MinDay_Config_SSRFlag}"
	
	# 查看文件中配置
	local file_conf="/etc/login.defs"
	local cmd="cat ${file_conf}"
	eval ${cmd} | grep "$flag_str"
	eval ${cmd} | grep -v "^#" | grep -Eq "${true_str}"
	CommRetParse_LTFLIB "${cmd}查询,要求${true_str}" "False"

	eval ${cmd} | grep -v "^#" | grep -Ev "${true_str}" | grep "${false_str}"
	CommRetParse_LTFLIB "${cmd}查询,不存在'${true_str}'以外的值" "False" "yes"
}


## TODO ：可接受的最小密码长度
#
testcase_3(){
	local flag_str="^PASS_MIN_LEN"

	# 错误选项
        local false_str="${flag_str}"
	# 正确选项
       	local true_str="${flag_str}[ 	]+${Passwd_MinLen_Config_SSRFlag}"
	
	# 查看文件中配置
	local file_conf="/etc/login.defs"
	local cmd="cat ${file_conf}"
	eval ${cmd} | grep "$flag_str"
	eval ${cmd} | grep -v "^#" | grep -Eq "${true_str}"
	CommRetParse_LTFLIB "${cmd}查询,要求${true_str}" "False"

	eval ${cmd} | grep -v "^#" | grep -Ev "${true_str}" | grep "${false_str}"
	CommRetParse_LTFLIB "${cmd}查询,不存在'${true_str}'以外的值" "False" "yes"
}


## TODO ：密码过期警告天数
#
testcase_4(){
	local flag_str="^PASS_WARN_AGE"

	# 错误选项
        local false_str="${flag_str}"
	# 正确选项
       	local true_str="${flag_str}[ 	]+${Passwd_WarnAge_Config_SSRFlag}"
	
	# 查看文件中配置
	local file_conf="/etc/login.defs"
	local cmd="cat ${file_conf}"
	eval ${cmd} | grep "$flag_str"
	eval ${cmd} | grep -v "^#" | grep -Eq "${true_str}"
	CommRetParse_LTFLIB "${cmd}查询,要求${true_str}" "False"

	eval ${cmd} | grep -v "^#" | grep -Ev "${true_str}" | grep "${false_str}"
	CommRetParse_LTFLIB "${cmd}查询,不存在'${true_str}'以外的值" "False" "yes"
}


#----------------------------------------------#

source "${LIB_LTFLIB}"
Main_LTFLIB $@
